package com.engine.modules.ytht.controller;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.baseEntity.ResponseEntity.ResponseMsg;
import com.engine.modules.ytht.entity.FarmersFieldsEntity;
import com.engine.modules.ytht.util.BusinessCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.*;

/**
 * @author FY
 * @version 1.0.0
 * @ClassName MsgExchangeController.java
 * @Description TODO
 * @createTime 2021年12月30日
 */
@RestController
@Api(tags = "5、信息交换")
@ApiSort(value = 5)
@CrossOrigin
@RequestMapping("/MsgExchange")
@Slf4j
public class MsgExchangeController {


    @Autowired
    CacheManager cacheManager;

    @ApiOperation(value = "获取数据", notes = "获取后数据删除")
    @GetMapping(value = "/getData/{key}")
    @ApiOperationSupport(order = 1)
    public ResponseMsg getData(@PathVariable String key) {
        try {
            Object o = cacheManager.getCache("msgExchangeCache").get(key).get() ;
            cacheManager.getCache("msgExchangeCache").evict(key);
            return ResponseMsg.success("读取成功").addData(o);
        } catch (Exception e) {
            return ResponseMsg.success("未读取到数据");
        }
    }

    @ApiOperation(value = "存储数据", notes = "存储后3000s删除")
    @PostMapping(value = "/putData/{key}")
    @ApiOperationSupport(order = 2)
    public ResponseMsg putData(@PathVariable String key,
                               @RequestBody String json) {
        try {
            Object object = JSONObject.parse(json);
            cacheManager.getCache("msgExchangeCache").put(key, object);
            return ResponseMsg.success("存储成功").addData(key);
        } catch (JSONException ex) {
            return ResponseMsg.success("json格式错误");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsg.error(e.getMessage());
        }
    }
    @ApiOperation(value = "获取数据（加密businessCode）", notes = "获取后数据删除，将对businessCode进行验证\n" +
            "environment参数（prod：生产环境，dev：开发环境，test：测试环境）")
    @GetMapping(value = "/getDataEncrypted/{businessCode}")
    @ApiOperationSupport(order = 3)
    public ResponseMsg getDataEncrypted(@PathVariable String businessCode) {
        try {
            String checkResult=BusinessCode.checkBusinessCode(businessCode);
            if(checkResult!="") {
                Object o = cacheManager.getCache("msgExchangeCache").get(businessCode).get();
                cacheManager.getCache("msgExchangeCache").evict(businessCode);
                return ResponseMsg.success("读取成功").addData(o);
            }
            else
                return ResponseMsg.fail("businessCode验证未通过");
        } catch (Exception e) {
            return ResponseMsg.success("未读取到数据");
        }
    }

    @ApiOperation(value = "存储数据（加密businessCode）", notes = "存储后3000s删除，将对businessCode进行验证\n"+
            "environment参数（prod：生产环境，dev：开发环境，test：测试环境）")
    @PostMapping(value = "/putDataEncrypted/{businessCode}")
    @ApiOperationSupport(order = 4)
    public ResponseMsg putDataEncrypted(@PathVariable String businessCode,
                                        @RequestBody String json) {
        try {
            String checkResult=BusinessCode.checkBusinessCode(businessCode);
            if(checkResult!="")
            {
//                Object object = JSONObject.parse(json);
                JSONObject object=JSONObject.parseObject(json);
                if(checkResult!="true")
                  object.put("identity",checkResult);
                cacheManager.getCache("msgExchangeCache").put(businessCode, object);
                return ResponseMsg.success("存储成功").addData(businessCode);
            }
            else
                return ResponseMsg.fail("businessCode验证未通过");
        } catch (JSONException ex) {
            return ResponseMsg.success("json格式错误");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsg.error(e.getMessage());
        }
    }
}
